
// store.ts
import { defineStore } from 'pinia';
import { empGetUserData } from '../api/login'
import { copy } from '../utils/date'
import router from "../router";
function recursion(item: any, url: any) {
  if (item.children) {
      recursion(item.children[0], url)
  } else {
      url.push(item.idxUri)
  }

  return url
}
function filterArr(data) {
  data.map(item => {
    if (item.classify==3&&item.children) {
      filterArr(item.children)
    } else {
      delete item.children
    }
  })
  return data
}
// 扁平化数据的递归函数 
function flattenData(data) {
  const result = [];
      data.forEach(item => {
        if(item.children){
          result.push(...flattenData(item.children));
        }else{
          result.push(item);
        }
      });
  return result;
}
const user = {
  userVO: {},
  list: []
}
type State = {
  active: number | string,
  user: user,
  tabItem: tabItem,
  url: string,
  tabs: tabs[],
  tagView:any[],
  menu:any[],
  menuList:any[],
  btnList:any[]
}
type user = {
  userVO: any,
  list: any
}
type tabItem = {
  name: string,
  icon: string,
}
type tabs = {
  name: string,
  icon: any,
  activeIcon: any,
  id: number,
  children: any,
  tab:number|string
}
export const useStore = defineStore({
  id: 'counter',
  state: (): State => ({
    active: 1,
    user: copy(user),
    tabItem: {
      name: '',
      icon: '',
    },
    url: '/system/dictionary/index',
    tabs: [
      {
        name: "系统管理",
        icon: new URL('../assets/system.png', import.meta.url),
        activeIcon: new URL('../assets/systemTive.png', import.meta.url),
        id: 1,
        children: [],
        tab:0,
      },
      {
        name: "业务进件",
        icon: new URL('../assets/business.png', import.meta.url),
        activeIcon: new URL('../assets/businessTive.png', import.meta.url),
        id: 2,
        children: [],
        tab:1,
      },
      {
        name: "待办任务",
        icon: new URL('../assets/agent.png', import.meta.url),
        activeIcon: new URL('../assets/agentTive.png', import.meta.url),
        id: 3,
        children: [],
        tab:2,
      },
      {
        name: "业务管理",
        icon: new URL('../assets/service.png', import.meta.url),
        activeIcon: new URL('../assets/serviceTive.png', import.meta.url),
        id: 4,
        children: [],
        tab:3,
      },
      {
        name: "报表管理",
        icon: new URL('../assets/report.png', import.meta.url),
        activeIcon: new URL('../assets/reportTive.png', import.meta.url),
        id: 5,
        children: [],
        tab:4,
      },

    ],
    menu: [],
    tagView: [],
    menuList:[],
    btnList:[],
  }),
  getters: {
  },
  actions: {
    //获取用户信息
    getUser() {
      empGetUserData({}).then((res: any) => {
        this.user = res
        this.ergodicMenu()
      })
    },
    //从登录过来存储信息
    setInfo(data: any) {
      this.user = copy(data)
      this.menuList=filterArr(data.list)
  
      this.btnList =flattenData(this.user.list)
      // console.log(this.btnList);
      this.ergodicMenu()
    },
    //组装菜单数据结构
    ergodicMenu() {
      this.tabs = this.tabs.filter((item: any) => {
        return this.menuList.some((item1: any) => item.id == item1.id)
      })
      this.tabs.map((item: any) => {
        this.menuList.map((item1: any) => {
          if (item.id == item1.id) {
            item.children = item1.children
          }
        })

      })
      this.menu = this.tabs[0].children
      this.tabItem.name = this.tabs[0].name
      this.tabItem.icon = this.tabs[0].activeIcon
      let url: any = []
      url = recursion(this.menu[0], url)
       router.push(url[0])
    },
    updateActive(data) {
      this.tabItem.name = data.item.name
      this.tabItem.icon = data.item.activeIcon
      this.active = data.item.tab
      this.url = data.url
      this.menu = data.item.children
    },
    filterList(arr: any, url: string,) {
      arr.map((item: any) => {
        if (item.idxUri == url) {
          this.active = item.tab
        } else {
          if (item.children) {
            this.filterList(item.children, url)
          }
        }
      })
    },
    updateUrl(url) {
      this.url = url
     if(url!='/login'){
      this.filterList(this.menuList, url)
      
      let index = this.menuList.findIndex(item => item.tab == this.active)
      this.menu = this.menuList[index].children
      this.tabItem.name = this.menuList[index].name
      this.tabItem.icon = this.tabs[index].activeIcon
     }
    },
    setPath(to: any) {//根据路由跳转获取当前url信息进行缓存到store
      if (!this.tagView.some(item => item.path == to.path)) {
        let num = 7
        // if (window.innerWidth == 1920) {
        //   num = 15
        // }
        if (this.tagView.length > num) {
          this.tagView.splice(1, 1)
        }
        if (to.meta.title) {
          this.tagView.push({ query: to.query, path: to.path, title: to.meta.title })
        }
      } else {
        this.tagView.map(item => {
          if (item.path == to.path) {
            item.query = to.query
          }
        })
      }
    },
    deleteTag(data: any) {

      if (data.index == 0 || this.tagView.length < 2) return
      this.tagView.splice(data.index, 1)
      if (data.tag.path == this.url) {
        // const router = useRouter()
        router.push({ path: this.tagView[data.index - 1].path, query: this.tagView[data.index - 1].query })
      }
    },
    deletePath(path: string) {
      let index = 0
      this.tagView.map((em: any, dex: number) => {
        if (em.path == path) {
          index = dex
        }
      })
      if (this.tagView.length < 2) return
      this.tagView.splice(index, 1)
    },
  },
  // 指定持久化的状态
  persist: {
    enabled: true,
    strategies: [
      {
        // 指定持久化的key
        key: 'myStore',
        // 指定持久化的存储类型，可以是localStorage或sessionStorage
        storage: localStorage,
      },
    ],
  },
})
